%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modify main_str to match path on your machine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main_str = 'C:\Users\Marco\Dropbox\Housing_Brainstorming\Information_disclosure';
% main_str = 'L:\agargano\Dropbox\Housing_Brainstorming\Information_disclosure';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load_str = [main_str,'\Replication_code_RFS\Model\Output\Last'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FontSize=18;
MarkSize=9;
XlabelSize = 16;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Load

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sel_obj_plot = 0; % generates Figure 4 ``naive bidders'' using subjective surplus
% sel_obj_plot = 1; % generates Figure 5 ``non-naive bidders'' using subjective surplus
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sel_j_vec = 23; % select calibration with \mu_{\epsilon} = 0 and \sigma_{\epsilon} = 8%
n_j       = size(sel_j_vec,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load results based on ``objective'' surplus calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load([load_str,'\Results_out_3_Nash_Loop_Obj.mat']);

eq_Erev_mat_obj      = eq_Erev_mat;   
eq_EbidW_mat_obj     = eq_EbidW_mat;
eq_EbidW_obj_mat_obj = eq_EbidW_obj_mat;
eq_Esales_mat_obj    = eq_Esales_mat;
eq_Psales_mat_obj    = eq_Psales_mat;
eq_Nall_mat_obj      = eq_Nall_mat;
eq_EbidN_mat_obj     = eq_EbidN_mat;
eq_p_hcost0_mat_obj  = eq_p_hcost0_mat;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load results based on ``subjective'' surplus calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load([load_str,'\Results_out_3_Nash_Loop.mat']);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_vec      = start_bid_scale-1; % vector of values of values
biasM_grid = unique(bias_grid_mat(:,1));
biasS_grid = unique(bias_grid_mat(:,2));
n_Mbias    = size(biasM_grid,1); % vector of values of mu_{\epsilon}
n_Sbias    = size(biasS_grid,1); % vector of values of sigma_{\epsilon}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figures for calibration with \mu_{\epsilon} = 0 and \sigma_{\epsilon} = 8%

for j = 1:n_j

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    figure(4+sel_obj_plot);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if sel_obj_plot == 0
        eq_Erev_vec      = eq_Erev_mat(:,sel_j_vec(j));   
        eq_EbidW_vec     = eq_EbidW_mat(:,sel_j_vec(j));
        eq_EbidW_obj_vec = eq_EbidW_obj_mat(:,sel_j_vec(j));
        eq_Esales_vec    = eq_Esales_mat(:,sel_j_vec(j));
        eq_Psales_vec    = eq_Psales_mat(:,sel_j_vec(j));
        eq_Nall_vec      = eq_Nall_mat(:,sel_j_vec(j));
        eq_EbidN_vec     = eq_EbidN_mat(:,sel_j_vec(j));
        eq_p_hcost0_vec  = eq_p_hcost0_mat(:,sel_j_vec(j));
    elseif sel_obj_plot == 1
        eq_Erev_vec      = eq_Erev_mat_obj(:,sel_j_vec(j));   
        eq_EbidW_vec     = eq_EbidW_mat_obj(:,sel_j_vec(j));
        eq_EbidW_obj_vec = eq_EbidW_obj_mat_obj(:,sel_j_vec(j));
        eq_Esales_vec    = eq_Esales_mat_obj(:,sel_j_vec(j));
        eq_Psales_vec    = eq_Psales_mat_obj(:,sel_j_vec(j));
        eq_Nall_vec      = eq_Nall_mat_obj(:,sel_j_vec(j));
        eq_EbidN_vec     = eq_EbidN_mat_obj(:,sel_j_vec(j));
        eq_p_hcost0_vec  = eq_p_hcost0_mat_obj(:,sel_j_vec(j));
    end

    %%%%%%%%%%%%%%%
    subplot(3,2,1);
    %%%%%%%%%%%%%%%
    temp_max=NaN(length(eq_Erev_vec),1); temp_max(eq_Erev_vec==max(eq_Erev_vec))=max(eq_Erev_vec);
    temp_eq_Erev_vec=eq_Erev_vec; temp_eq_Erev_vec(eq_Erev_vec==max(eq_Erev_vec))=NaN;
    plot(q_vec,temp_eq_Erev_vec/1000,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    plot(q_vec,temp_max/1000,'*','MarkerSize',MarkSize,'MarkerFaceColor','red'); grid on; 
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    title('E[Seller Revenue]')
    ylabel('1,000 $');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(3,2,2);
    %%%%%%%%%%%%%%%
    if sel_obj_plot == 0
        plot(q_vec,eq_EbidW_obj_vec/1000,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;    
        plot(q_vec,eq_EbidW_vec/1000,'Linestyle','none','marker','s','MarkerSize',MarkSize,'MarkerFaceColor','red'); grid on;    
        xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
        ylabel('1,000 $');
        title('E[Bidder Surplus]'); legend({'Objective','Subjective'});
        set(gca,'fontsize',FontSize);
        xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    elseif sel_obj_plot == 1
        plot(q_vec,eq_EbidW_obj_vec/1000,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue');
        xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
        ylabel('1,000 $');
        title('E[Bidder Surplus]');
        set(gca,'fontsize',FontSize);
        xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    end    
    %%%%%%%%%%%%%%%    
    subplot(3,2,3);
    %%%%%%%%%%%%%%%
    plot(q_vec,(eq_Esales_vec/1000)+650,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('1,000 $');
    title('E[Sale Price|Sale]')
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%% 
    subplot(3,2,4);
    %%%%%%%%%%%%%%%
    plot(q_vec,100*eq_Psales_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('(%)');
    title('P(Sale)');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(3,2,5);
    %%%%%%%%%%%%%%%
    plot(q_vec,eq_Nall_vec.*eq_EbidN_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    title('E[Number of Bidders]');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  
    %%%%%%%%%%%%%%%
    subplot(3,2,6);
    %%%%%%%%%%%%%%%
    plot(q_vec,100*eq_EbidN_vec,'Linestyle','none','marker','d','MarkerSize',MarkSize,'MarkerFaceColor','blue'); hold on;
    xticks([-0.2:0.05:0.2]); xlim([-0.20,0.20]); grid on;
    ylabel('(%)');
    title('E[Fraction of Bidders]');
    set(gca,'fontsize',FontSize);
    xlabel('$\alpha$','interpreter','latex','fontsize',XlabelSize);  

 

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Find Equilibrium for Different Values of \mu_{\epsilon} and \sigma_{\epsilon}, from model in which bidders maximize ``subjective'' surplus (are naive)
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Allocate Memory
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_opt_vec            = nan(J,1);
eq_Erev_opt_vec      = nan(J,1);
eq_EbidW_opt_vec     = nan(J,1);
eq_EbidW_opt_obj_vec = nan(J,1);
eq_Esales_opt_vec    = nan(J,1);
eq_Psales_opt_vec    = nan(J,1);
eq_Nall_opt_vec      = nan(J,1);
eq_EbidN_opt_vec     = nan(J,1);
eq_p_hcost0_opt_vec  = nan(J,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
for j = 1:J % loop over values of \mu_{\epsilon} and \sigma_{\epsilon}

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    [~,opt_ind]            = max(eq_Erev_mat(:,j)); % find value of alpha that maximizes seller surplus

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Store Auction Outcomes
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    q_opt_vec(j)            = q_vec(opt_ind); 
    eq_Erev_opt_vec(j)      = eq_Erev_mat(opt_ind,j);
    eq_EbidW_opt_vec(j)     = eq_EbidW_mat(opt_ind,j);
    eq_EbidW_opt_obj_vec(j) = eq_EbidW_obj_mat(opt_ind,j);    
    eq_Esales_opt_vec(j)    = eq_Esales_mat(opt_ind,j); 
    eq_Psales_opt_vec(j)    = eq_Psales_mat(opt_ind,j);
    eq_Nall_opt_vec(j)      = eq_Nall_mat(opt_ind,j);
    eq_EbidN_opt_vec(j)     = eq_EbidN_mat(opt_ind,j);
    eq_p_hcost0_opt_vec(j)  = eq_p_hcost0_mat(opt_ind,j);

end    


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Reshape Outcomes in Matrices, for which \mu_{\epsilon} represents rows
% and \sigma_{\epsilon} columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_opt_mat            = reshape(q_opt_vec,n_Mbias,n_Sbias); 
eq_Erev_opt_mat      = reshape(eq_Erev_opt_vec,n_Mbias,n_Sbias);
eq_EbidW_opt_mat     = reshape(eq_EbidW_opt_vec,n_Mbias,n_Sbias);
eq_EbidW_opt_obj_mat = reshape(eq_EbidW_opt_obj_vec,n_Mbias,n_Sbias);    
eq_Esales_opt_mat    = reshape(eq_Esales_opt_vec,n_Mbias,n_Sbias); 
eq_Psales_opt_mat    = reshape(eq_Psales_opt_vec,n_Mbias,n_Sbias);
eq_Nall_opt_mat      = reshape(eq_Nall_opt_vec,n_Mbias,n_Sbias);
eq_EbidN_opt_mat     = reshape(eq_EbidN_opt_vec,n_Mbias,n_Sbias);
eq_p_hcost0_opt_mat  = reshape(eq_p_hcost0_opt_vec,n_Mbias,n_Sbias);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Same As Previous Section, but using outcomes from model in which bidders maximize ``objective'' surplus (are non-naive)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_opt_vec_obj            = nan(J,1);
eq_Erev_opt_vec_obj      = nan(J,1);
eq_EbidW_opt_vec_obj     = nan(J,1);
eq_EbidW_opt_obj_vec_obj = nan(J,1);
eq_Esales_opt_vec_obj    = nan(J,1);
eq_Psales_opt_vec_obj    = nan(J,1);
eq_Nall_opt_vec_obj      = nan(J,1);
eq_EbidN_opt_vec_obj     = nan(J,1);
eq_p_hcost0_opt_vec_obj  = nan(J,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
for j = 1:J

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    [~,opt_ind]            = max(eq_Erev_mat_obj(:,j));

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    q_opt_vec_obj(j)            = q_vec(opt_ind); 
    eq_Erev_opt_vec_obj(j)      = eq_Erev_mat_obj(opt_ind,j);
    eq_EbidW_opt_vec_obj(j)     = eq_EbidW_mat_obj(opt_ind,j);
    eq_EbidW_opt_obj_vec_obj(j) = eq_EbidW_obj_mat_obj(opt_ind,j);    
    eq_Esales_opt_vec_obj(j)    = eq_Esales_mat_obj(opt_ind,j); 
    eq_Psales_opt_vec_obj(j)    = eq_Psales_mat_obj(opt_ind,j);
    eq_Nall_opt_vec_obj(j)      = eq_Nall_mat_obj(opt_ind,j);
    eq_EbidN_opt_vec_obj(j)     = eq_EbidN_mat_obj(opt_ind,j);
    eq_p_hcost0_opt_vec_obj(j)  = eq_p_hcost0_mat_obj(opt_ind,j);

end    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
q_opt_mat_obj            = reshape(q_opt_vec_obj,n_Mbias,n_Sbias); 
eq_Erev_opt_mat_obj      = reshape(eq_Erev_opt_vec_obj,n_Mbias,n_Sbias);
eq_EbidW_opt_mat_obj     = reshape(eq_EbidW_opt_vec_obj,n_Mbias,n_Sbias);
eq_EbidW_opt_obj_mat_obj = reshape(eq_EbidW_opt_obj_vec_obj,n_Mbias,n_Sbias);    
eq_Esales_opt_mat_obj    = reshape(eq_Esales_opt_vec_obj,n_Mbias,n_Sbias); 
eq_Psales_opt_mat_obj    = reshape(eq_Psales_opt_vec_obj,n_Mbias,n_Sbias);
eq_Nall_opt_mat_obj      = reshape(eq_Nall_opt_vec_obj,n_Mbias,n_Sbias);
eq_EbidN_opt_mat_obj     = reshape(eq_EbidN_opt_vec_obj,n_Mbias,n_Sbias);
eq_p_hcost0_opt_mat_obj  = reshape(eq_p_hcost0_opt_vec_obj,n_Mbias,n_Sbias);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Figure

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(3);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sel_y_obs   = 3; % select \mu_{\epsilon}=0
n_y_obs     = size(sel_y_obs,2);

for n = 1:n_y_obs

    subplot(3,2,1);
    plot(biasS_grid,100*q_opt_mat(sel_y_obs(n),:)','-b','Linewidth',6); hold on;
    plot(biasS_grid,100*q_opt_mat_obj(sel_y_obs(n),:)',':k','Linewidth',6); hold on;    
    if n == n_y_obs
        ylabel('(%)');
        xlabel('$\sigma_{\epsilon}$','interpreter','latex','fontsize',20); 
        legend({'Naive Bidders','Non-Naive Bidders'},'interpreter','latex','fontsize',20);
        title('$\alpha^{*}$','interpreter','latex'); set(gca,'fontsize',FontSize); grid on;
    end

    subplot(3,2,2);
    plot(biasS_grid,eq_Erev_opt_mat(sel_y_obs(n),:)'/1000,'-b','Linewidth',6); hold on;
    plot(biasS_grid,eq_Erev_opt_mat_obj(sel_y_obs(n),:)'/1000,':k','Linewidth',6); hold on;    
    if n == n_y_obs
        ylabel('AUD 1,000');
        xlabel('$\sigma_{\epsilon}$','interpreter','latex','fontsize',20);
        title('E[Seller Revenue]'); set(gca,'fontsize',FontSize); grid on;
    end

    subplot(3,2,3);
    plot(biasS_grid,eq_EbidW_opt_mat(sel_y_obs(n),:)'/1000,'-b','Linewidth',6); hold on;
    plot(biasS_grid,eq_EbidW_opt_obj_mat_obj(sel_y_obs(n),:)'/1000,':k','Linewidth',6); hold on;    
    if n == n_y_obs
        ylabel('AUD 1,000'); ylim([-8,5]);
        xlabel('$\sigma_{\epsilon}$','interpreter','latex','fontsize',20);
        title('E[Bidder Surplus] (Subjective)'); set(gca,'fontsize',FontSize); grid on;
    end

    subplot(3,2,4);
    plot(biasS_grid,eq_EbidW_opt_obj_mat(sel_y_obs(n),:)'/1000,'-b','Linewidth',6); hold on;
    plot(biasS_grid,eq_EbidW_opt_obj_mat_obj(sel_y_obs(n),:)'/1000,':k','Linewidth',6); hold on;    
    if n == n_y_obs
        ylabel('AUD 1,000'); ylim([-8,5]);
        xlabel('$\sigma_{\epsilon}$','interpreter','latex','fontsize',20);
        title('E[Bidder Surplus] (Objective)'); set(gca,'fontsize',FontSize); grid on;
    end

    subplot(3,2,5);
    plot(biasS_grid,650+(eq_Esales_opt_mat(sel_y_obs(n),:)'/1000),'-b','Linewidth',6); hold on;
    plot(biasS_grid,650+(eq_Esales_opt_mat_obj(sel_y_obs(n),:)'/1000),':k','Linewidth',6); hold on;    
    if n == n_y_obs
        ylabel('AUD 1,000');
        xlabel('$\sigma_{\epsilon}$','interpreter','latex','fontsize',20);
        title('E[Sales Price|Selling]'); set(gca,'fontsize',FontSize); grid on;
    end

    subplot(3,2,6);
    plot(biasS_grid,100*eq_Psales_opt_mat(sel_y_obs(n),:)','-b','Linewidth',6); hold on;
    plot(biasS_grid,100*eq_Psales_opt_mat_obj(sel_y_obs(n),:)',':k','Linewidth',6); hold on;    
    if n == n_y_obs
        ylabel('(%)');
        xlabel('$\sigma_{\epsilon}$','interpreter','latex','fontsize',20);
        title('P[Sales]'); set(gca,'fontsize',FontSize); grid on;
    end

end


